<template>
  <div class="editor">

    <div ref="toolbar" class="toolbar"></div>
    <div ref="editor" class="text"></div>

  </div>
</template>
<script>
import { upload } from "@/api/webpage"
import E from "wangeditor";
export default {
  name: "editor",
  data() {
    return {
      editor: null,
      info_: null,
    };
  },
  props: {
    value: {
      type: String,
      default: "",
    },
    isClear: {
      type: Boolean,
      default: false,
    },
  },
  watch: {
    isClear(val) {
      // 触发清除文本域内容
      if (val) {
        this.editor.txt.clear();
        this.info_ = null;
      }
    },
    value: function (value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value);
      }
    },
  },
  mounted() {
    this.seteditor();
    this.editor.txt.html(this.value);
  },
  methods: {
    seteditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      this.editor.config.onchange = (html) => {
        this.info_ = html// 绑定当前逐渐地值
        this.$emit("editorchange", this.info_); 
      };
      this.editor.config.uploadImgShowBase64 = true 
    //   this.editor.config.customUploadImg = function (files, insert) {
    //     const file = new FormData()
    //     file.append('file',files[0])
    //       upload(file).then(res => {
    //         insert(process.env.VUE_APP_BASE_HOST+'/show'+res.data[0].address)
    //       })
    //   }
    //   console.log(this.editor.config.customUploadImg);

      this.editor.create();
    },
  },
};
</script>
<style scoped>
.editor {
  border: 1px solid #dcdfe6;
  min-height: 300px;
}
</style>